package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.stock.StockListResultVO;
import com.sansan.qiangji.VO.base.stock.StockVO;
import com.sansan.qiangji.entity.base.Stock;
import com.sansan.qiangji.entity.base.example.StockExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.StockAddForm;
import com.sansan.qiangji.form.base.StockEditForm;
import com.sansan.qiangji.form.base.StockListForm;
import com.sansan.qiangji.service.inter.base.StockService;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StockReal {
    @Autowired
    private StockService stockService;
    @Transactional
    public void deleteStock(Integer stockId){
        Stock stock = stockService.selectByPrimaryKey(stockId);
        if(stock==null){
            log.error("【货位删除】货位不存在，stockId={}",stockId);
            throw new ServiceException(ResultEnum.STOCK_IS_NOT_EXIST);
        }
        int row = stockService.deleteByPrimaryKey(stockId);
        if(row==0){
            log.error("【货位删除】货位删除失败，stockId={}",stockId);
            throw new ServiceException(ResultEnum.STOCK_DELETE_ERROR);
        }
    }

    @Transactional
    public void addStock(StockAddForm stockAddForm){
       
        Stock stock = new Stock();
        BeanUtils.copyProperties(stockAddForm,stock);
        int row = stockService.insert(stock);
        if(row==0){
            log.error("【货位新增】货位新增失败");
            throw new ServiceException(ResultEnum.STOCK_INSERT_ERROR);
        }
    }


    @Transactional
    public void editStock(StockEditForm stockEditForm){
        Stock stock = new Stock();
        BeanUtils.copyProperties(stockEditForm,stock);
        int row = stockService.updateByPrimaryKey(stock);
        if(row==0){
            log.error("【货位新增】货位新增失败");
            throw new ServiceException(ResultEnum.STOCK_INSERT_ERROR);
        }
    }

    public StockListResultVO stockList(StockListForm stockListForm){
        StockExample example = new StockExample();
        int pageNum = stockListForm.getPageNum();
        int pageSize = stockListForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Stock> stockList = stockService.selectByExample(example);
        PageResult<Stock> stockPageResult = PageUtil.getPageResult(new PageInfo<>(stockList));
        StockListResultVO stockListResultVO = new StockListResultVO();
        BeanUtils.copyProperties(stockPageResult,stockListResultVO);
        List<StockVO> stockVOS = new ArrayList<>();
        for(Stock stock : stockPageResult.getContent()){
            StockVO stockVO = new StockVO();
            BeanUtils.copyProperties(stock,stockVO);
            stockVOS.add(stockVO);
        }
        stockListResultVO.setStockVOS(stockVOS);
        return stockListResultVO;
    }
}
